package com.huawei.hms.framework.network.grs.b;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.grs.GrsBaseInfo;
import com.huawei.hms.framework.network.grs.b.b.b;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class e {
    private static volatile e a;
    private ExecutorService b = Executors.newCachedThreadPool();
    private Map<String, b> c = new ConcurrentHashMap(16);
    private final Object d = new Object();

    private e() {
    }

    public static e a() {
        if (a == null) {
            synchronized (e.class) {
                if (a == null) {
                    a = new e();
                }
            }
        }
        return a;
    }

    private void a(c cVar, com.huawei.hms.framework.network.grs.b bVar) {
        if (bVar == null) {
            return;
        }
        if (cVar == null) {
            Logger.v("RequestController", "GrsResponse is null");
            bVar.a();
            return;
        }
        Logger.v("RequestController", "GrsResponse is not null");
        bVar.a(cVar);
    }

    public c a(final GrsBaseInfo grsBaseInfo) {
        Future submit;
        String grsParasKey = grsBaseInfo.getGrsParasKey(false, true);
        synchronized (this.d) {
            b bVar = (b) this.c.get(grsParasKey);
            if (bVar == null || !bVar.b()) {
                Logger.v("RequestController", "hitGrsRequestBean == null");
                submit = this.b.submit(new Callable<c>(this) {
                    final /* synthetic */ e b;

                    public c a() {
                        return new b(grsBaseInfo).a(this.b.b);
                    }

                    public /* synthetic */ Object call() {
                        return a();
                    }
                });
                this.c.put(grsParasKey, new b(submit));
            } else {
                submit = bVar.a();
            }
        }
        try {
            return (c) submit.get();
        } catch (Throwable e) {
            Logger.w("RequestController", "when check result, find CancellationException, check others", e);
        } catch (Throwable e2) {
            Logger.w("RequestController", "when check result, find ExecutionException, check others", e2);
        } catch (Throwable e22) {
            Logger.w("RequestController", "when check result, find InterruptedException, check others", e22);
        }
        return null;
    }

    public void a(final GrsBaseInfo grsBaseInfo, final com.huawei.hms.framework.network.grs.b bVar) {
        this.b.submit(new Runnable(this) {
            final /* synthetic */ e c;

            public void run() {
                this.c.a(this.c.a(grsBaseInfo), bVar);
            }
        });
    }

    public void a(String str) {
        synchronized (this.d) {
            this.c.remove(str);
        }
    }
}
